package com.fuyao.cloud.personnel.problem.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fuyao.cloud.common.core.util.R;
import com.fuyao.cloud.common.data.resolver.ParamResolver;
import com.fuyao.cloud.common.excel.annotation.RequestExcel;
import com.fuyao.cloud.common.excel.annotation.ResponseExcel;
import com.fuyao.cloud.common.log.annotation.SysLog;
import com.fuyao.cloud.personnel.api.dto.ProblemRectifyDTO;
import com.fuyao.cloud.personnel.api.entity.ProblemRectify;
import com.fuyao.cloud.personnel.api.vo.RectifyThoseResponsibleVO;
import com.fuyao.cloud.personnel.api.vo.excel.ProblemRectifyExcelExportVO;
import com.fuyao.cloud.personnel.api.vo.excel.ProblemRectifyExcelVO;
import com.fuyao.cloud.personnel.problem.service.ProblemRectifyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 整改措施
 *
 * @author pigx code generator
 * @date 2022-04-26 10:14:04
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/rectify" )
@Api(value = "rectify", tags = "整改措施管理")
public class ProblemRectifyController {

    private final ProblemRectifyService problemRectifyService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param problemRectify 整改措施
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('problem_rectify_view')" )
    public R getFyProblemRectifyPage(Page page, ProblemRectify problemRectify) {
        return R.ok(problemRectifyService.page(page, Wrappers.query(problemRectify)));
    }

    /**
     * 分页查询(包含责任人)
     * @param page 分页对象
     * @param problemRectify 整改措施
     * @return
     */
    @ApiOperation(value = "分页查询(包含责任人)", notes = "分页查询(包含责任人)")
    @GetMapping("/page/thoseResponsible" )
    @PreAuthorize("@pms.hasPermission('problem_rectify_view')" )
    public R pageRectifyThoseResponsibleVO(Page page, RectifyThoseResponsibleVO problemRectify) {
        return R.ok(problemRectifyService.pageRectifyThoseResponsibleVO(page, problemRectify));
    }

    /**
     * 用于组件查询
     * @param page 分页对象
     * @param problemRectify 整改措施
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/component/page" )
    @PreAuthorize("@pms.hasPermission('problem_rectify_view')" )
    public R getRectifyComponentPage(Page page, ProblemRectify problemRectify) {
        return R.ok(problemRectifyService.page(page, Wrappers.query(problemRectify)));
    }

    /**
     * 通过id查询整改措施
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('problem_rectify_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(problemRectifyService.getById(id));
    }

    /**
     * 新增整改措施
     * @param problemRectify 整改措施
     * @return R
     */
    @ApiOperation(value = "新增整改措施", notes = "新增整改措施")
    @SysLog("新增整改措施" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('problem_rectify_add')" )
    public R save(@RequestBody ProblemRectify problemRectify) {
        problemRectifyService.save(problemRectify);
        return R.ok(problemRectify.getId());
    }

    /**
     * 新增整改措施
     * @param problemRectify 整改措施
     * @return R
     */
    @ApiOperation(value = "新增整改措施", notes = "新增整改措施")
    @SysLog("新增整改措施" )
    @PostMapping("/saveAndRectifyDetailed")
    @PreAuthorize("@pms.hasPermission('problem_rectify_add')")
    public R saveAndRectifyDetailed(@RequestBody ProblemRectifyDTO problemRectify) {
        return R.ok(problemRectifyService.saveRectifyAndResponsPerson(problemRectify));
    }

    /**
     * 修改整改措施
     * @param problemRectify 整改措施
     * @return R
     */
    @ApiOperation(value = "修改整改措施", notes = "修改整改措施")
    @SysLog("修改整改措施" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('problem_rectify_edit')" )
    public R updateById(@RequestBody ProblemRectify problemRectify) {
        return R.ok(problemRectifyService.updateById(problemRectify));
    }

    /**
     * 通过id删除整改措施
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除整改措施", notes = "通过id删除整改措施")
    @SysLog("通过id删除整改措施" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('problem_rectify_del')" )
    public R removeById(@PathVariable Long id) {
        return R.ok(problemRectifyService.removeById(id));
    }

    /**
     * 导出excel 表格
     * @param problemRectify 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('problem_rectify_export')" )
    public List<ProblemRectifyExcelExportVO> export(RectifyThoseResponsibleVO problemRectify) {
        Page page = new Page(1, ParamResolver.getInt("MAX_PAGE_SIZE",999999));
        return problemRectifyService.getProblemRectifyExcelListVO(page, problemRectify);
    }

    /**
     * 导入
     * @return R
     */
    @PostMapping("/import")
    @PreAuthorize("@pms.hasPermission('problem_rectify_export')")
    public R importProblemRectify(@RequestExcel List<ProblemRectifyExcelVO> excelVOList, BindingResult bindingResult) {
        return problemRectifyService.importProblemRectify(excelVOList, bindingResult);
    }

    /**
     * 导出excel 表格
     * @param id 查询条件
     * @return excel 文件流
     */
    @GetMapping("/getRegularlyRemind/{id}")
    public R getRegularlyRemind(@PathVariable("id") Long id) {
        return R.ok(problemRectifyService.getRegularlyReminds(id));
    }

    @GetMapping("/getInforName/{id}")
    public R getInforName(@PathVariable("id") Long id){
        return R.ok(problemRectifyService.getInforName(id));
    }
}
